package com.qyhl.webtv.module_broke.scoop;


import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.broke.ScoopTopicBean;
import com.qyhl.webtv.commonlib.entity.circle.ScoopListBean;
import com.qyhl.webtv.module_broke.common.BrokeUrl;

import java.util.HashMap;
import java.util.List;

/**
 * Created by qyhl on 2017/12/10.
 */

public class ScoopHomeModel implements ScoopHomeContract.ScoopHomeModel {
    private ScoopHomePresenter mPresenter;

    ScoopHomeModel(ScoopHomePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取话题
     */
    @Override
    public void getTopic(final boolean isFirst) {
        EasyHttp.get(BrokeUrl.GET_SCOOP)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<List<ScoopTopicBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(3, "暂无任何内容话题！");
                        } else {
                            if (isFirst) {
                                mPresenter.setError(4, "解析出错，加载失败！");
                            } else {
                                mPresenter.setError(5, "解析出错，加载失败！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<ScoopTopicBean> scoopTopicBeans) {
                        if (scoopTopicBeans != null && scoopTopicBeans.size() > 0) {
                            mPresenter.setTopic(scoopTopicBeans);
                        } else {
                            mPresenter.setError(2, "暂无任何内容！");
                        }
                    }
                });


    }

    /**
     * 获取话题下相关内容
     *
     * @param username
     * @param topicId
     * @param brokeId
     */
    @Override
    public void getDatas(String username, String topicId, String brokeId) {
        final boolean isMore;
        isMore = !"0".equals(brokeId);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("userName", username);
        if (!"-1".equals(topicId)) {//-1 时  表示请求全部
            hashMap.put("plateId", topicId);
        }
        hashMap.put("brokeId", brokeId);
        hashMap.put("siteId", CommonUtils.getInstance().getSite() + "");

        EasyHttp.get(BrokeUrl.GET_SCOOP_LIST)
                .params(hashMap)
                .execute(new SimpleCallBack<List<ScoopListBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            if (isMore) {
                                mPresenter.setDataError(2, "no more");
                            } else {
                                mPresenter.setDataError(3, "暂无任何相关内容！");
                            }
                        } else {
                            if (isMore) {
                                mPresenter.setDataError(4, "加载失败！");
                            } else {
                                mPresenter.setDataError(5, "加载失败！");
                            }
                        }
                    }

                    @Override
                    public void onSuccess(List<ScoopListBean> scoopListBeans) {
                        if (scoopListBeans != null && scoopListBeans.size() > 0) {
                            mPresenter.setDatas(scoopListBeans, isMore);
                        } else {
                            if (isMore) {
                                mPresenter.setDataError(2, "no more");
                            } else {
                                mPresenter.setDataError(3, "暂无任何相关内容！");
                            }
                        }
                    }
                });
    }
}
